var server = function(config, $, app) {

	var getToken = function() {
		return user.getUserInfo();
	}

	var getHeader = function() {
		var token = getToken();
		var headers = {
			'Authorization': 'Bearer ' + token.access_token
		};
		return headers;
	}

	// 刷新token
	var refreshToken = function(success, error) {
		mui.toast('刷新refresh_token ' + getToken().refresh_token);
		$.ajax({
			type: "post",
			url: config.getLogin,
			data: {
				grant_type: 'refresh_token',
				refresh_token: getToken().refresh_token
			},
			success: function(r) {
				localStorage.setItem('logininfo', JSON.stringify(r));
				if(success) {
					success();
				}
			},
			error: function() {
				if(error) {
					error();
				}
			}
		});
	};

	// 处理ajax异常
	var errorHandle = function(xhr, type, errorThrown, cbError) {
		if(cbError) {
			cbError(xhr);
		}
	};

	//	会议模块
	var meeting = {

		//在线、预约会议
		getOnline: function(success, error) {
			$.ajax({
				type: "get",
				url: config.getMeetingOnline,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error)
				}
			});
		},

		//	历史会议
		getHistory: function(params, success, error) {
			$.ajax({
				type: 'get',
				data: params,
				url: config.getMeetingPager,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	会议室日程
		getRoomSchedules: function(params, success, error) {
			$.ajax({
				type: "get",
				data: params,
				url: config.getMeetingRoomSchedules,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	预约会议
		bookMeeting: function(params, success, error) {
			$.ajax({
				type: "post",
				data: params,
				url: config.getReserve,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	取消会议
		cancelMeeting: function(id, _creator, success, error) {
			$.ajax({
				type: "post",
				data: '',
				url: config.getUserCancel + '?meetingId=' + id + '&userid=' + _creator,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	预约会议
		getBookMeeting: function(params, success, error) {
			$.ajax({
				type: "get",
				data: params,
				url: config.getUserOrders,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	会议详情
		getDetailMeeting: function(params, success, error) {
			$.ajax({
				type: "get",
				data: params,
				url: config.getMeetingDetail,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	查看会议聊天历史记录
		getMeetingMsgs: function(params, success, error) {
			$.ajax({
				type: "get",
				data: params,
				url: config.getMeetingMsgs,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	配置NVR
		setNvrConfig: function(success, error) {
			$.ajax({
				type: "get",
				url: config.nvrConfig,
				headers: getHeader(),
				success: function(r) {
					if(r) {
						localStorage.setItem('nvrconfig', JSON.stringify(r));
					}
				},
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	获取NVR配置
		getNvrConfig: function() {
			var strNvrConfig = localStorage.getItem('nvrconfig');
			return JSON.parse(strNvrConfig);
		},

		//	会议点赞 
		setLikes: function(params, success, error) {
			$.ajax({
				type: "post",
				data: params,
				url: config.getLike,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	开始会议
		setStartMeeting: function(params1, params2, success, error) {
			$.ajax({
				type: "post",
				url: config.getStart + '?channel=' + params1 + '&meetingid=' + params2,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	结束会议
		setStopMeeting: function(params1, params2, success, error) {
			$.ajax({
				type: "post",
				url: config.getStop + '?channel=' + params1 + '&meetingid=' + params2,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	获取指定用户
		getUserBy: function(id) {
			var str = localStorage.getItem('contacts');
			if(!str || str == '') return null;
			var json = JSON.parse(str);
			for(var i = 0; i < json.length; i++) {
				if(json[i].id == id) {
					return json[i];
				}
			}
			return null;
		}
	};

	//	用户模块
	var user = {

		//	验证用户登录
		loginUser: function(params, success, error) {
			$.ajax({
				type: "post",
				data: params,
				url: config.getLogin,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	获取登录用户登录信息
		getLoginUser: function() {
			var getuserInfo = JSON.parse(localStorage.getItem('userinfo')) || "{}";
			return getuserInfo;
		},

		//	设置用户登录信息
		setUserInfo: function(params, success, error) {
			$.ajax({
				type: "get",
				data: params,
				url: config.getUserInfo,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	获取用户登录信息
		getUserInfo: function() {
			var userLoginInfo = JSON.parse(localStorage.getItem('logininfo')) || "";
			return userLoginInfo;
		},

		//	设置通讯录存储
		setContactsInfo: function(success, error) {
			$.ajax({
				type: "get",
				url: config.getContactsInfo,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	获取通讯录数据
		getContactsInfo: function() {
			var getContactsInfo = JSON.parse(localStorage.getItem('contacts')) || "{}";
			return getContactsInfo;
		},

		//	获取人员列表数据
		getPeopleInfo: function(success, error) {
			$.ajax({
				type: "get",
				data: '',
				url: config.getContactsInfo,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	修改用户密码
		updateUserPwd: function(params, success, error) {
			$.ajax({
				type: "post",
				data: params,
				url: config.getChangepassword,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	更新用户资料
		saveUserInfo: function(params, success, error) {
			$.ajax({
				type: "post",
				data: params,
				url: config.getUpdate,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	清空消息(个人消息中心)
		setClearMessages: function(params, success, error) {
			$.ajax({
				type: "post",
				url: config.getClearMessages + '?userid=' + params,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	标记为已读消息(个人消息中心)
		setReadMessages: function(params, success, error) {
			$.ajax({
				type: "post",
				url: config.getReadMessages + '?userid=' + params,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	获取部门清单
		getDeparInfo: function(params, success, error) {
			$.ajax({
				type: "get",
				url: config.getDepts + '?type=' + params,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		}

	}

	//	消息模块
	var message = {

		//	获取消息中心数据
		getMsg: function(params, success, error) {
			$.ajax({
				type: "get",
				data: params,
				url: config.getUserMessages,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},

		//	获取未读消息
		getUnreadMsg: function(params, success, error) {
			$.ajax({
				type: "get",
				data: params,
				url: config.getUserUnreadMessages,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		}
	}

	//	文件操作模块
	var files = {
		//	文件上传(暂未使用)
		uploads: function(success, error) {
			$.ajax({
				type: "post",
				url: config.getUpload,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		},
	}

	//	服务器模块
	var serverconfig = {

		//	设置服务器配置
		setConfigInfo: function(info) {
			localStorage.setItem('configinfo', JSON.stringify(info));
		},

		//	获取服务器配置信息
		getConfigInfo: function() {
			var configInfo = JSON.parse(localStorage.getItem('configinfo')) || "";
			return configInfo;
		},

		//	获取APP版本信息
		checkAppVersion: function(type, version, success, error) {
			$.ajax({
				type: "get",
				data: '',
				url: config.getVersion + '?type=' + type + '&version=' + version,
				headers: getHeader(),
				success: success,
				error: function(xhr, type, errorThrown) {
					errorHandle(xhr, type, errorThrown, error);
				}
			});
		}

	}

	return {
		refreshToken: refreshToken,
		meeting: meeting,
		user: user,
		message: message,
		files: files,
		serverconfig: serverconfig
	};
}(config, mui, app);